﻿using System;
using System.Web.UI;
using Dimebrain.TweetSharp.Fluent;
using TweetSayingsLibrary.App;
using TweetSayingsLibrary.App.Session;
using TweetSayingsLibrary.Data.Entity;


namespace TweetSayings
{
    public partial class OAuthLogin : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Current.SessionTrack.ViewingPage("Home.aspx");
        }

        protected void tickNow_Update(object sender, EventArgs e)
        {
            tickNow.Enabled = false;
            
            if (Request["oauth_token"] != null && Request["oauth_verifier"] != null)
            {
                lock (this)
                {                    
                    Current.SessionTrack.ApiCaller.AuthenticateResponse(Request["oauth_token"], Request["oauth_verifier"]);                    
                    Visitor.Login(Current.SessionTrack.ApiCaller.Request(t => t.Account().VerifyCredentials(), false));
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }

            // Kick off background processing
            if ((Current.Visitor != null))
            {
                IInnerSession innerSession = Current.SessionTrack;
                //TweetSayingsLibrary.Accounts.TwitterUser user = Current.Visitor.TwitterUser;

                //ThreadStart work = () => user.BackgroundProcess(innerSession);
                //Thread thread = new Thread(work);
                //thread.Start();
            }

            Response.Redirect("Home.aspx");
        }
    }
}